import pygame
import helpers
import math

class Entity(pygame.sprite.Sprite):
    
    # The entities position. Should be a Numpy array.
    position = None
    
    # The entities velocity. Should be a Numpy array.
    velocity = None
    
    # The mass of the entity. Used in calculating acceleration.
    mass = None
    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        
    def update_position(self, dtime, force):
        '''
        Update position and velocity given a force vector.
        '''
        force = helpers.truncate( force, self.max_force )
        acceleration = force / self.mass
        
        self.velocity += acceleration*dtime
        self.velocity = helpers.truncate( self.velocity, self.max_speed )
        
        self.position += self.velocity*dtime
    
    def draw_healthbar(self, view_port):
        '''
        Draw healthbar above entity. Only shown if the entity is damaged.
        '''
        BAR_HEIGHT = 3
        BAR_VOFFSET = 2
        if self.health < self.max_health:
            pygame.draw.line(view_port.screen, (255,255,255), 
                 (self.rect.left,self.rect.top-BAR_VOFFSET), 
                 (self.rect.right,self.rect.top-BAR_VOFFSET), BAR_HEIGHT)
            pygame.draw.line(view_port.screen, 
                 (0,255,0), (self.rect.left,self.rect.top-BAR_VOFFSET), 
                 (self.rect.left+(self.health/self.max_health)*self.rect.width,
                  self.rect.top-BAR_VOFFSET), BAR_HEIGHT)